package cc.idoone.lucene;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.nio.file.Paths;

/**
 * 包含中文的索引
 *
 * @author hemj
 * @since 2017/07/18
 */
public class SmartcnIndexer {

    private String[] ids = {"1", "2", "3"};
    private String citys[] = {"青岛", "南京", "上海"};
    private String descs[] = {
            "青岛是一个漂亮的城市。",
            "南京是一个文化的城市。",
            "上海是一个繁华的城市。"
    };

    /**
     * 实例化indexerWriter
     *
     * @return
     * @throws Exception
     */
    private IndexWriter getWriter(String indexDir) throws Exception {
        Directory dir = FSDirectory.open(Paths.get(indexDir));
        SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer(); // 中文分词器
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(dir, iwc);
        return writer;
    }

    /**
     * 获取indexDir
     *
     * @param indexDir
     * @throws Exception
     */
    private void index(String indexDir) throws Exception {
        IndexWriter writer = getWriter(indexDir);
        for (int i = 0; i < ids.length; i++) {
            Document doc = new Document();
            doc.add(new StringField("id", ids[i], Field.Store.YES));
            doc.add(new StringField("city", citys[i], Field.Store.YES));
            doc.add(new TextField("desc", descs[i], Field.Store.YES));
            writer.addDocument(doc);
        }
        writer.close();
    }

    public static void main(String[] args) throws Exception {
        new SmartcnIndexer().index("D:\\lucene\\dataindex2");
        System.out.println("Success Indexer");
    }
}
